package checkers3d.storage;

import java.io.*;
import java.util.*;
import checkers3d.logic.*;

/**
 * Saves all of the instant data replay and loads the instant replay to the
 * appropriate objects and variables.
 *
 * @author Sean Keel
 */
public class DataManagerInstantReplay {

    /**
     * The relative path to the location where saved replays are stored.
     */
    static public final String LOCATION_SAVEREPLAYS = ".//replay/";

    /**
     * Saves each state of the board after a move has been made by saving each
     * board tile location, including which BoardObject is located there and
     * who owns that BoardObject.
     *
     * @param x The x coordinate from a Point3D.
     * @param y The y coordinate from a Point3D.
     * @param z The coordinate from a Point3D.
     * @param object The object that was stored at the Point3D location.
     */
    static public void saveInstantReplayData(String playerHome, String playerVisitor, PlayingSurface3D playingSurface3D) throws IOException{
        String filename = (DataManagerInstantReplay.LOCATION_SAVEREPLAYS
                + playerHome + "-" + playerVisitor + "-" + "REPLAY.txt");
        FileWriter formattedFile = new FileWriter(filename, true);
        BufferedWriter out = new BufferedWriter(formattedFile);
        String replayState = new String();

        out.write("C\tIgnore!");
        out.newLine();

        DataManagerUtil.writePlayingSurface3D(out, playingSurface3D);

        out.close();
    }

    /**
     * Loads the instant replay data from the InstantReplayData file.
     */
    static public LinkedList loadInstantReplayData(String filename, Player home, Player visitor) throws IOException{
        FileReader fr = new FileReader(filename);
        Scanner scan = new Scanner(fr);
        LinkedList playingSurfaces = new LinkedList();

        System.out.println("\nReading instant replay file.");

        //set graphics for the home player.
        home.setCheckerRR("player1tokenchecker.png");
        home.setKingRR("player1tokenking.png");
        home.setWallRR("generictokenwall.png");
        home.setSafeRR("player1tokensafe.png");
        home.setMineRR("player1tokenmine.png");

        //set graphics for the visiting player.
        visitor.setCheckerRR("player2tokenchecker.png");
        visitor.setKingRR("player2tokenking.png");
        visitor.setWallRR("generictokenwall.png");
        visitor.setSafeRR("player2tokensafe.png");
        visitor.setMineRR("player2tokenmine.png");

        System.out.println("\nDone reading instant replay file.");

        while(scan.hasNext())
            playingSurfaces.add(DataManagerUtil.readPlayingSurface3D(scan, home, visitor));
            
        scan.close();
        System.out.print("\nThere are " + playingSurfaces.size() + " replay states.");
        return playingSurfaces;
    }

    /**
     * Erases the InstantDataReplay file.
     */
    static public void eraseInstantReplayData(String filename) throws IOException{
        FileWriter formattedFile = new FileWriter(filename, false);
        formattedFile.close();
    }
}